/*
 * Created by JFormDesigner on Tue Jun 22 13:55:39 CST 2021
 */

package com.huidian.gitbatchprocessing.ui.dialog;

import com.huidian.gitbatchprocessing.config.CredentialsConfig;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author JFormDesigner
 */
public class UserPasswordInputDialog extends JDialog {
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JTextField textFieldUsername;
    private JLabel label2;
    private JTextField textFieldPassword;
    private JPanel buttonBar;
    private JCheckBox checkBoxRememberMe;
    private JButton okButton;
    public UserPasswordInputDialog(Frame owner, Component parentComponent) {
        super(owner, true);
        this.setAlwaysOnTop(Boolean.TRUE);
        initComponents();
        this.setLocationRelativeTo(parentComponent);
    }

    public CredentialsConfig getCredentialsConfig() {
        if (StringUtil.isEmpty(textFieldUsername.getText())) {
            Messages.showErrorDialog("用户名不能为空", "错误");
            throw new RuntimeException();
        }
        if (StringUtil.isEmpty(textFieldPassword.getText())) {
            Messages.showErrorDialog("密码不能为空", "错误");
            throw new RuntimeException();
        }
        return new CredentialsConfig(textFieldUsername.getText(), textFieldPassword.getText());
    }

    /**
     * 是否选中持否持久化用户名密码
     *
     * @return
     */
    public boolean endurance() {
        return checkBoxRememberMe.isSelected();
    }

    private void okButtonActionPerformed(ActionEvent e) {
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        textFieldUsername = new JTextField();
        label2 = new JLabel();
        textFieldPassword = new JTextField();
        buttonBar = new JPanel();
        checkBoxRememberMe = new JCheckBox();
        okButton = new JButton();

        //======== this ========
        setTitle("\u8ba4\u8bc1\u4fe1\u606f");
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("\u7528\u6237\u540d\uff1a");
                contentPanel.add(label1);
                label1.setBounds(10, 25, 55, 20);
                contentPanel.add(textFieldUsername);
                textFieldUsername.setBounds(60, 20, 205, textFieldUsername.getPreferredSize().height);

                //---- label2 ----
                label2.setText("\u5bc6\u7801\uff1a");
                contentPanel.add(label2);
                label2.setBounds(10, 70, 55, 20);
                contentPanel.add(textFieldPassword);
                textFieldPassword.setBounds(60, 60, 205, 30);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for (int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout) buttonBar.getLayout()).columnWidths = new int[]{0, 85, 80};
                ((GridBagLayout) buttonBar.getLayout()).columnWeights = new double[]{1.0, 0.0, 0.0};

                //---- checkBoxRememberMe ----
                checkBoxRememberMe.setText("\u8bb0\u4f4f\u5bc6\u7801");
                checkBoxRememberMe.setSelected(true);
                buttonBar.add(checkBoxRememberMe, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));

                //---- okButton ----
                okButton.setText("\u786e\u5b9a");
                okButton.addActionListener(e -> okButtonActionPerformed(e));
                buttonBar.add(okButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
